package com.yxx.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.yxx.annotation.AccessLimit;
import com.yxx.entityModel.vo.request.CodeRequest;
import com.yxx.entityModel.vo.request.LoginRequest;
import com.yxx.entityModel.vo.Result.ResponseResult;
import com.yxx.entityModel.vo.request.RegisterRequest;
import com.yxx.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author yuxiangxun
 * @date 2025/1/19
 * @apiNote
 */
@RestController
@Slf4j
@Api(tags = "登录接口模块")
public class LoginController {
    @Resource
    private LoginService loginService;

    /**
     * 用户登录
     * @param login
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public ResponseResult<String> login(@Validated @RequestBody LoginRequest login){
        return  ResponseResult.success(loginService.login(login));
    }

    /**
     * 用户退出
     */
    @SaCheckLogin
    @ApiOperation(value = "用户退出")
    @GetMapping("/logout")
    public ResponseResult<?> logout() {
        StpUtil.logout();
        return ResponseResult.success();
    }

    /**
     * 发送邮箱验证码
     * @param username
     * @return
     */
    @AccessLimit(seconds = 60, maxCount = 1)
    @ApiOperation(value = "发送邮箱验证码")
    @GetMapping("/code")
    public ResponseResult<?> sendCode(String username) {
        loginService.sendCode(username);
        return ResponseResult.success();
    }

    /**
     * 用户邮箱注册
     * @param registerRequest
     * @return
     */
    @ApiOperation(value = "用户邮箱注册")
    @PostMapping("/register")
    public ResponseResult<?> register(@Validated @RequestBody RegisterRequest registerRequest){
        loginService.register(registerRequest);
        return ResponseResult.success();
    }

    /**
     * Gitee 登录
     * @param codeRequest
     * @return
     */
    @ApiOperation(value = "Gitee 登录")
    @PostMapping("/oauth/gitee")
    public ResponseResult<String> giteeLogin(@RequestBody CodeRequest codeRequest){
        return ResponseResult.success(loginService.giteeLogin(codeRequest));
    }


    /**
     * Github登录
     *
     * @param data 第三方code
     * @return {@link ResponseResult<String>} Token
     */
    @ApiOperation(value = "Github登录")
    @PostMapping("/oauth/github")
    public ResponseResult<String> githubLogin(@RequestBody CodeRequest data) {
        return ResponseResult.success(loginService.githubLogin(data));
    }

    /**
     * QQ登录
     *
     * @param data 第三方code
     * @return {@link ResponseResult<String>} Token
     */
    @ApiOperation(value = "QQ登录")
    @PostMapping("/oauth/qq")
    public ResponseResult<String> qqLogin(@Validated @RequestBody CodeRequest data) {
        return ResponseResult.success(loginService.qqLogin(data));
    }
}
